package cn.edu.hqu.javae.util;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import cn.edu.hqu.javae.domain.Course;
import cn.edu.hqu.javae.repository.CourseRepository;

@Component
public class CourseByIdConverter implements Converter<String,  Course> {

	  private  CourseRepository  courseRepo;

	  @Autowired
	  public  CourseByIdConverter( CourseRepository  courseRepo) {
	    this. courseRepo =  courseRepo;
	  }
	  
	  @Override
	  public  Course convert(String id) {
		  Optional< Course> optionalCourse =  courseRepo.findById(id);
			return optionalCourse.isPresent() ?
				   optionalCourse.get() : null;
	  }

	}